<?php

namespace app\distribution\controller;

use app\common\controller\Adminbase;
use app\model\DistributionSetting;
use app\model\User as UserModel;

class User extends Adminbase
{
    public function index()
    {
        $model = 'User';

        if ($this->request->isAjax()) {
            $map      = $this->buildParamsMap();
            $page     = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : 20;

            $list  = UserModel::where($map)
                ->field('*')
                ->page($page, $pageSize)
                ->order('id', 'asc')
                ->select();
            $total = UserModel::where($map)->count();

            if ($list) {
                $info  = DistributionSetting::field('id, type, value')->where('uuid', 'setting')->find();
                $value = json_decode($info['value'], true);
                $text  = [1 => "<span class='layui-badge layui-bg-green'>是</span>", 0 => "<span class='layui-badge'>否</span>"];
                foreach ($list as &$item) {
                    $level_info    = $item['level'] == 2 ? $value[1] : $value[0];
                    $item['level'] = $item['bind_status'] == 1 ? $level_info['name'] : '';
                    $item['is_tk'] = $text[$item['bind_status']];
                }
            }

            $result = ["code" => 0, "count" => $total, "data" => $list];
            return json($result);
        }

        //生成列表数据
        $builder = new \app\common\controller\ListBuilder();
        $builder
            ->addSearchItem('nickname', 'text', '推客昵称')
            ->addSearchItem('mobile', 'text', '手机号')
            ->addSearchItem('bind_status', 'select', '是否推客', [1 => '是', 0 => '否'])
            ->setDelBtn(false)
            ->setAddBtn(false)
            ->setDelAllBtn(false)
            ->setDataUrl()
            ->setFormUrl('form')
            ->setLimit()
            ->setModel($model)
            ->addTableCheckBox()
            ->setSwitchTpl('switchTpl', 'status')
            ->addTableColumn('id', '用户ID', null, null, true)
            ->addTableColumn('token', '用户标识', null, null, true)
//            ->addTableColumn('id', '上级用户id')
            ->addTableColumn('nickname', '昵称')
            ->addTableColumn('mobile', '手机号')
            ->addTableColumn('is_tk', '是否推客')
            ->addTableColumn('level', '推客身份')
            ->addTableColumn('create_time', '创建时间')
            ->addTableOperation()
            ->returnForm();

        $this->assign('module', 'app');
        return $this->fetch('admin@common/lists');
    }

    public function form()
    {
        $model = 'User';

        $id = $this->request->param('id/d');
        if (isset($id) && $id > 0) {
            $info = UserModel::get($id);
            $this->assign('id', $id);
            $this->assign('action', 2);
        }

        $setting = DistributionSetting::field('id, type, value')->where('uuid', 'setting')->find();
        $value   = json_decode($setting['value'], true);
        $level   = [];
        foreach ($value as $key => $val) {
            $level[$key + 1] = $val['name'];
        }

        $builder = new \app\common\controller\FormBuilder();
        $builder
            ->setModel($model)
            ->setSuccessUrl()
            ->addFormItem('level', 'radio', '推客等级', '', $level)
            ->setFormData($info)
            ->returnForm();
        $this->assign('module', 'app');
        $this->assign('is_verify', 0);
        return $this->fetch('admin@common/form');
    }
}
